Public Class frmSearchPolice
#Region "Declare Variables"
    Private m_eScreenMode As eScreenMode
    Private m_Police As New PoliceHealthClass.Police
    Private m_ManagePoliceControl As New PoliceHealthClass.ManageReferrenceControl
    Private IsPostBack As Boolean = False

    Private cbbStoolExamResultHistory As ComboBox

    Delegate Sub delLoadDatagridviewPolice(ByVal intSelectRowIndex As Integer)
#End Region

#Region "Enumuration"
    Private Enum eScreenMode As Integer
        Initial
        View
        Add
        Edit
    End Enum
#End Region

#Region "General Functions"
    Private Sub SetScreenMode(ByVal ScreenMode As eScreenMode)
        m_eScreenMode = ScreenMode
        Select Case ScreenMode
            Case eScreenMode.Initial
                PoliceHealthClass.Utilities.EnableControl(False, btnSave, btnCancel, btnEdit, btnChangeOrganization, txtName, txtSurName, mtbCitizen_ID)
                PoliceHealthClass.Utilities.EnableControl(True, btnSearch, chkName, chkSurName, chkCitizen_ID)

                dgvPolice.Enabled = True

                If Not Me.IsPostBack Then

                    ConnectionString.ShowLoadingForm()
                    Me.Invoke(New delLoadDatagridviewPolice(AddressOf LoadDatagridviewPolice), 0)
                    ConnectionString.TerminateLoadingForm()

                    'LoadDatagridviewPolice()
                    Me.IsPostBack = True
                End If

            Case eScreenMode.View
                PoliceHealthClass.Utilities.EnableControl(False, btnSave, btnCancel, btnChangeOrganization)
                PoliceHealthClass.Utilities.EnableControl(True, btnSearch, btnEdit, chkName, chkSurName, chkCitizen_ID)

                PoliceHealthClass.Utilities.EnableControl(chkName.Checked, txtName)
                PoliceHealthClass.Utilities.EnableControl(chkSurName.Checked, txtSurName)
                PoliceHealthClass.Utilities.EnableControl(chkCitizen_ID.Checked, mtbCitizen_ID)

                dgvPolice.Enabled = True
            Case eScreenMode.Add


            Case eScreenMode.Edit
                PoliceHealthClass.Utilities.EnableControl(True, btnSave, btnCancel, btnChangeOrganization)
                PoliceHealthClass.Utilities.EnableControl(False, btnEdit, chkName, chkSurName, txtName, txtSurName, btnSearch, mtbCitizen_ID, chkCitizen_ID)

                dgvPolice.Enabled = False
        End Select


    End Sub


    Private Function VerifyInputData() As Boolean
        Return m_ManagePoliceControl.verifyUpdate()
    End Function

    Private Sub LoadDatagridviewPolice(Optional ByVal intSelectRowIndex As Integer = 0)
        dgvPolice.Enabled = True

        If chkCitizen_ID.Checked Then
            m_Police.LoadPoliceDetails(mtbCitizen_ID.Text.Replace("-", ""))
        Else
            m_Police.LoadSearchPolice(IIf(chkName.Checked, txtName.Text, ""), IIf(chkSurName.Checked, txtSurName.Text, ""))
        End If



        Dim intRowCount As Integer = 0
        dgvPolice.Rows.Clear()
        While m_Police.NextRecord
            dgvPolice.Rows.Add()
            dgvPolice.Rows(intRowCount).Cells("Order").Value = intRowCount + 1
            dgvPolice.Rows(intRowCount).Cells("name_full").Value = m_Police.prename.prename_short_tha & m_Police.name & " " & m_Police.surname
            dgvPolice.Rows(intRowCount).Cells("citizen_id").Value = m_Police.citizen_id
            dgvPolice.Rows(intRowCount).Cells("organization_name_tha").Value = m_Police.organization.organization_name_tha
            dgvPolice.Rows(intRowCount).Cells("OrganizationID").Value = m_Police.organization.organization_id
            dgvPolice.Rows(intRowCount).Cells("work_status_name_tha").Value = m_Police.work_status.work_status_name_tha

            dgvPolice.Rows(intRowCount).Selected = False

            intRowCount += 1
        End While

        If intSelectRowIndex < dgvPolice.Rows.Count Then
            dgvPolice.Rows(intSelectRowIndex).Selected = True
            dgvPolice.CurrentCell = dgvPolice.Rows(intSelectRowIndex).Cells(0)
        End If


        If dgvPolice.Rows.Count > 0 Then
            SetScreenMode(eScreenMode.View)
            LoadDataForm()
        Else
            If Me.IsPostBack Then
                SetScreenMode(eScreenMode.Initial)
            End If
            ClearFormData()
        End If
    End Sub

    Private Sub LoadDataForm(Optional ByVal RowIndex As Integer = -1)
        'Load Form
        If dgvPolice.SelectedRows.Count > 0 Then
            If RowIndex >= 0 Then
                lblOrganization.Text = dgvPolice.Rows(RowIndex).Cells("organization_name_tha").Value
                lblOrganization.Tag = dgvPolice.Rows(RowIndex).Cells("OrganizationID").Value
            Else
                lblOrganization.Text = dgvPolice.SelectedRows(0).Cells("organization_name_tha").Value
                lblOrganization.Tag = dgvPolice.SelectedRows(0).Cells("OrganizationID").Value
            End If
        Else
            lblOrganization.Text = dgvPolice.Rows(0).Cells("organization_name_tha").Value
            lblOrganization.Tag = dgvPolice.Rows(0).Cells("OrganizationID").Value
        End If
    End Sub

    Private Sub ClearFormData()
        lblOrganization.Text = ""
        lblOrganization.Tag = ""
        PoliceHealthClass.Utilities.EnableControl(False, btnEdit)
    End Sub






#End Region

#Region "Event Handling Functions"
    Private Sub frmPolice_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'CMU_KPI_PHASE_1DataSet.tbError_Logging' table. You can move, or remove it, as needed.
        SetScreenMode(eScreenMode.Initial)
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        If dgvPolice.SelectedRows.Count > 0 Then
            LoadDataForm()
            SetScreenMode(eScreenMode.View)
        Else
            SetScreenMode(eScreenMode.Initial)
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim SelectIndex As Integer = 0

        If m_eScreenMode = eScreenMode.Edit Then
            If VerifyInputData() Then

                If dgvPolice.SelectedRows.Count > 0 Then
                    SelectIndex = dgvPolice.SelectedRows(0).Index
                End If

                m_Police.citizen_id = dgvPolice.SelectedRows(0).Cells("citizen_id").Value
                m_Police.organization.organization_id = lblOrganization.Tag
                m_Police.commentary = dgvPolice.SelectedRows(0).Cells("PoliceCommentary").Value
                m_Police.update_date = Date.Now
                m_Police.update_user = PoliceHealthSystem.ConnectionString._username

                m_ManagePoliceControl.showUpdateResult(m_Police.updatePoliceOrganization(m_Police))

                ConnectionString.ShowLoadingForm()
                Me.Invoke(New delLoadDatagridviewPolice(AddressOf LoadDatagridviewPolice), dgvPolice.SelectedRows(0).Index)
                ConnectionString.TerminateLoadingForm()

                'LoadDatagridviewPolice(dgvPolice.SelectedRows(0).Index)
            End If
        End If
    End Sub


    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        SetScreenMode(eScreenMode.Edit)
    End Sub

    Private Sub dgvPolice_RowEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvPolice.RowEnter
        LoadDataForm(e.RowIndex)
    End Sub
#End Region

    Private Sub chkName_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkName.CheckedChanged
        PoliceHealthClass.Utilities.EnableControl(chkName.Checked, txtName)
    End Sub

    Private Sub chkSurName_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkSurName.CheckedChanged
        PoliceHealthClass.Utilities.EnableControl(chkSurName.Checked, txtSurName)
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click

        ConnectionString.ShowLoadingForm()
        Me.Invoke(New delLoadDatagridviewPolice(AddressOf LoadDatagridviewPolice), 0)
        ConnectionString.TerminateLoadingForm()

        'LoadDatagridviewPolice()
    End Sub

    Private Sub btnChangeOrganization_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChangeOrganization.Click
        Dim f As New frmChangeOrganization
        f.Owner = Me
        f.ShowDialog()
    End Sub

    Private Sub chkCitizen_ID_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkCitizen_ID.CheckedChanged
        PoliceHealthClass.Utilities.EnableControl(chkCitizen_ID.Checked, mtbCitizen_ID)
    End Sub
End Class